<?
require_once('./lib/authentication.lib.php');

if (!$_SESSION["user"]) die("You have to be logged in");

?>
<HTML>
<HEAD><TITLE>Insert stadium</TITLE></HEAD>

<BODY>

<?

if (isset($_GET["champ"]))
{
	$champ = mysql_escape_string($_GET["champ"]);
?>
	<H1>Insert stadium for Championship</H1>
	<FORM ACTION="insert_stadium.php" METHOD="POST" NAME="main_form">
	<INPUT TYPE="hidden" NAME="champ" VALUE="<?echo $champ?>">
	<P>Name:
	<INPUT TYPE="text" NAME="stadium">
	<P><INPUT TYPE="submit" NAME="submit" value="Send">
<?
}
else if (isset($_POST["champ"]) and isset($_POST["stadium"]))
{
	$champ = mysql_escape_string($_POST["champ"]);
	$stadium = mysql_escape_string($_POST["stadium"]);

	$query = "INSERT INTO Stadium VALUES ('', '$stadium')";
	mysql_query($query) or die("Could not insert referee");

	$query = "SELECT LAST_INSERT_ID();";
	$result = mysql_query($query);
	$stadium_id = mysql_result($result, 0, 0);
?>

<SCRIPT>
<!--
main_form = window.opener.document.main_form;
stadium = new Option('<?echo $stadium?>', '<?echo $stadium_id?>');

for (i = 0; i < main_form.elements.length; i++)
{
	s = main_form.elements[i];
	if (s.name == "stadium")
	{
		for (j = 0; j < s.options.length - 1; ++j)
		{
			if (s.options[j].text > stadium.text) break;
		}
		prev = s.options[j];
		s.options[j] = stadium;
		s.options[j].selected = true;
		for (k = j + 1; k < s.options.length; ++k)
		{
			curr = s.options[k];
			s.options[k] = prev;
			prev = curr;
		}
		s.options[k] = prev;
	}
}
window.close();
-->
</SCRIPT>

<?
}
?>

</BODY>
</HTML>
